package com.sptpc.demo.service.impl;

import com.sptpc.demo.model.Drink;
import com.sptpc.demo.model.Order;
import com.sptpc.demo.repository.DrinkRepository;
import com.sptpc.demo.repository.OrderRespository;
import com.sptpc.demo.service.IOrderService;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderJpaService implements IOrderService {
    @Autowired
    private OrderRespository orderRespository;
    @Autowired
    private DrinkRepository drinkRespository;
    @Override
    public void addNewOrder(Order order, List<Integer> items){
        order.setState(0);
        List<Drink> drinks=drinkRespository.findAllById(items);
        order.setItems(drinks);
//        BigDecimal price=BigDecimal.ZERO;
//        for(Drink drink : drinks){
//            price = price.add(drink.getPrice().getAmount());
//        }
        drinks.stream().map(drink -> drink.getPrice().getAmount())
                .reduce(BigDecimal.ZERO,(p1,p2) -> p1= p1.add(p2));

        order.setPrice(Money.of(CurrencyUnit.of("CNY"),price));
        orderRespository.save(order);
    }
    @Override
    public List<Order> findOrdersByCustomer(String  customer){
        Order order =new Order();
    }
}
